package com.sunbim.rsrv.controller.v1;


import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sunbim.rsrv.common.exception.RequestException;
import com.sunbim.rsrv.common.response.R;
import com.sunbim.rsrv.params.common.Ids;
import com.sunbim.rsrv.pojo.Storage;
import com.sunbim.rsrv.service.StorageService;
import com.sunbim.rsrv.vo.storage.StorageVo;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yuhui
 * @since 2021-01-30
 */
@RestController
@RequestMapping("/api/v1/storage")
public class StorageController {
    @Autowired
    StorageService storageService;

    @SneakyThrows
    @SaCheckRole("admin")
    @PostMapping("/upload")
    public StorageVo upload(
            @RequestParam(value = "relics_id", required = false) Integer relicsId,
            @RequestParam(value = "user_id", required = false) Integer userId,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "desc", required = false) String desc,
            @RequestParam("file") MultipartFile file
    ) {
        return storageService.upload(null, userId, relicsId, type, desc, file);
    }

    @SaCheckRole("admin")
    @PostMapping("/update")
    public StorageVo update(
            @RequestParam(value = "id", required = true) Integer id,
            @RequestParam(value = "relics_id", required = false) Integer relicsId,
            @RequestParam(value = "user_id", required = false) Integer userId,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "desc", required = false) String desc,
            @RequestParam("file") MultipartFile file
    ) {
        if (ObjectUtil.isNull(id)) throw new RequestException("更新文件必须指定一个文件id");
        if (!storageService.idExit(id)) throw new RequestException("文件id不存在");
        return storageService.update(id, userId, relicsId, type, desc, file);
    }

    @GetMapping("/getStorageByRelicsIdAndType")
    public List<StorageVo> getStorageByRelicsIdAndType(
            @RequestParam(value = "relics_id") Integer relicsId,
            @RequestParam(value = "type") String type
    ) {
        return storageService.getListByRelicsIdAndType(relicsId, type);
    }

    @SaCheckRole("admin")
    @GetMapping("/delete")
    public String delete(@RequestParam(value = "id") int id) {
        return storageService.delete(id);
    }


    @GetMapping("/select/reports")
    public List<Map<String, Object>> getReports(
            @RequestParam(value = "relics_id") Integer relicsId
    ) {
        return storageService.getReports(relicsId);
    }

    @Transactional
    @SaCheckRole("admin")
    @PostMapping("/upload/reports")
    public String uploadReport(
            @RequestParam(value = "relics_id", required = false) Integer relicsId,
            @RequestParam("report") MultipartFile report
    ) {
        storageService.uploadReport(relicsId, report);
        return "上传成功";
    }

    @SneakyThrows
    @GetMapping("/report/{id}")
    public void getReport(
            @PathVariable("id") Integer id,
            HttpServletResponse response
    ) {
        ServletOutputStream outputStream = response.getOutputStream();
        Storage storage = storageService.getById(id);
        String filepath = storage.getStoragePath();
        InputStream in = new FileInputStream(filepath);
        IOUtils.copy(in, outputStream);
        in.close();
        outputStream.close();
    }

    // 2022-2-27 yuhui previewpdf
    // 2022-3-10 yuhui previewpdf modify
    @SneakyThrows
    @RequestMapping("/showPdf/{id}")
    public void showPdf(@PathVariable("id") Integer id, HttpServletResponse response) {

        response.setContentType("application/pdf");
        FileInputStream in;
        OutputStream out;
        try {
            Storage storage = storageService.getById(id);
            String filepath = storage.getStoragePath();
            in = new FileInputStream(new File(filepath));
            out = response.getOutputStream();
            byte[] b = new byte[512];
            while ((in.read(b)) != -1) {
                out.write(b);
            }
            out.flush();
            in.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @GetMapping("/eva")
    public StorageVo getEva() {
        return storageService.getEva();
    }
}

